<?php
/**
 * 用户中心模型[QQ]
 * 
 * @version $Id: LoginQQ.php 408 2012-08-15 09:56:09Z linsir123 $
 */
class modelLoginQQ implements ILogin 
{
	/**
	 * @var 配置信息
	 */
	public $conf;
	
	/**
	 * 构造函数
	 */
	public function __construct()
	{
		$this->conf = App::O('member.qq');
		@session_start();
	}
	
	/**
	 * 取得授权类型
	 */
	public function getType()
	{
		return 'qq';
	}
	
	/**
	 * 取得唯一标识
	 */
	public function getKey()
	{
		return $_SESSION['qq']["openid"];
	}
	
	/**
	 * 取得用户呢称
	 */
	public function getNick()
	{
		$url = "https://graph.qq.com/user/get_user_info?"
			. "access_token=" . $_SESSION['qq']["access_token"]
			. "&oauth_consumer_key=" . $this->conf["appid"]
			. "&openid=" . $_SESSION['qq']["openid"]
			. "&format=json";
			
		$info = $this->_queryUrl($url);
		$data = json_decode($info, true);
		return $data['nickname'];
	}
	
	/**
	 * 登录URL
	 */
	public function getLoginUrl()
	{
		$_SESSION['qq']['state'] = md5(uniqid(rand(), true));
		return "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" 
			. $this->conf['appid'] . "&redirect_uri=" . urlencode($this->_getCallback())
			. "&scope=".$this->conf['scope'] . "&state=" . $_SESSION['qq']['state'];
	}
	
	/**
	 * 回调处理
	 */
	public function callbackHandler()
	{
		/*
		if ($_REQUEST['state'] != $_SESSION['qq']['state'])
			exit("The state does not match. You may be a victim of CSRF.");
			*/
			
		if ( ! isset($_SESSION['qq']['access_token'])) {
			/// 取得token
			$tokenUrl = $this->_getTokenUrl();
			$str = $this->_queryUrl($tokenUrl);
			if (strpos($str, "callback") !== false) {
				var_dump($str);
				exit();
			}
			
			$params = array();
			parse_str($str, $params);
			$_SESSION['qq']["access_token"] = $params["access_token"];
			
			/// 取得openid
			$graphUrl = $this->_getGraphUrl();
			$str = $this->_queryUrl($graphUrl);
			if (strpos($str, "callback") !== false) {
				$lpos = strpos($str, "(");
				$rpos = strrpos($str, ")");
				$str  = substr($str, $lpos + 1, $rpos - $lpos -1);
			}
			
			$user = json_decode($str);
			if (isset($user->error)) {
				var_dump($user);
				exit();
			}
			
			///
			$_SESSION['qq']["openid"] = $user->openid;
		}
		
		M::user()->loginOauth($this);
	}
	
	/**
	 * 回调URL
	 */
	public function _getCallback()
	{
		$url = str_replace(':80', '', lpUrl::__($this->conf['callback'], null, true));
		///
		if (isset($_GET['taobao_url'])) 
			$url .= '?taobao_url='. urlencode($_GET['taobao_url']);
			
		return $url;
	}
	
	/**
	 * 授权URL
	 */
	function _getTokenUrl()
	{
		return "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id="
			. $this->conf['appid']. "&redirect_uri=" . urlencode($this->_getCallback())
			. "&client_secret=" . $this->conf["appkey"]. "&code=" . $_REQUEST["code"];
	}
	
	/**
	 * 用户信息URL
	 */
	function _getGraphUrl()
	{
		return "https://graph.qq.com/oauth2.0/me?access_token=" 
			. $_SESSION['qq']["access_token"];
	}
	
	/**
	 * 页面抓取
	 */
	function _queryUrl($url, $vars = array())
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_FAILONERROR, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		if (is_array($vars) && ! empty($vars)) {
			$list = "";
			foreach ($vars as $k => $v) 
				$list .= "$k=" . urlencode($v) . "&"; 
				
			curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, substr($list,0,-1));
		}
		
		///
		$result = curl_exec($ch);
		curl_close($ch);
		return $result;
	}
}
